package br.unicamp.ic.mc906.kmeans.test;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import br.unicamp.ic.mc906.kmeans.KmeansInterface;

public class XPoint implements KmeansInterface<XPoint> {

	
	private double x;
	
	public XPoint(double x) {
		this.x = x;
	}
	
	@Override
	public double getDistance(XPoint element) {
		return Math.abs(this.x - element.getX()); 
	}
	
	@Override
	public XPoint centroidOfCluster(List<XPoint> elements) {
		double sum = 0.0;
		for (XPoint e : elements) {
			sum += e.getX();
		}
		return new XPoint(sum/elements.size());
	}

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}
 	
	@Override
	public String toString() {
		NumberFormat nf = new DecimalFormat();
		nf.setMaximumFractionDigits(2);
		return nf.format(this.x);
	}
}
